import streamlit as st
from link_checker import find_links, check_links
import requests

st.set_page_config(page_title="自动化测试小工具", layout="centered")
st.title("🛠️ 自动化测试小工具")

# Tab 1: 网页死链检测
with st.expander("网页死链检测", expanded=True):
    st.subheader("检测网页中的死链")
    url = st.text_input("请输入要检测的网页URL", key="url_input")
    if st.button("开始检测", key="check_links_btn"):
        if url:
            with st.spinner("正在检测所有链接..."):
                try:
                    links = find_links(url)
                    bad_links = check_links(links)
                    if bad_links:
                        st.error(f"检测到 {len(bad_links)} 个异常链接：")
                        for link, reason in bad_links:
                            st.write(f"- {link} → {reason}")
                    else:
                        st.success("所有链接均正常！")
                except Exception as e:
                    st.error(f"检测失败: {e}")
        else:
            st.warning("请输入URL")

# Tab 2: API返回数据展示
with st.expander("API返回数据展示", expanded=True):
    st.subheader("直接显示API返回的JSON数据")
    api_url = st.text_input("请输入API地址", key="api_url_input")
    if st.button("获取并显示数据", key="show_api_btn"):
        if api_url:
            try:
                resp = requests.get(api_url, timeout=10)
                resp.raise_for_status()
                data = resp.json()
                st.success("API返回数据如下：")
                st.json(data)
            except Exception as e:
                st.error(f"请求失败或返回内容不是JSON: {e}")
        else:
            st.warning("请输入API地址")

st.markdown("---")
st.caption("Made by symbol Zhuo | 支持网页死链检测与API数据展示的简单自动化工具 | 2025") 